﻿package code.utils.keymanager {
    import flash.events.*;
    import flash.display.*;

    public class KeyManager extends Sprite {

        public static const ALPHA_NUMERIC_KEY_DOWN:String = "alpha_numeric_key_down";

        private var combos:Array;
        private var onStage:Boolean;

        public function KeyManager(){
            this.onStage = false;
            this.combos = new Array();
            addEventListener(Event.ADDED_TO_STAGE, this.addedToStage);
            addEventListener(Event.REMOVED_FROM_STAGE, this.removedFromStage);
        }
        public function createKeyCombo(... _args):KeyCombo{
            var _local4:Key;
            var _local2:KeyCombo = new KeyCombo();
            var _local3:int;
            while (_local3 < _args.length) {
                _local4 = new Key(_args[_local3]);
                _local2.addKey(_local4);
                _local3++;
            };
            this.combos.push(_local2);
            return (_local2);
        }
        public function removeKeyCombo(_arg1:KeyCombo):void{
            var _local2:int;
            while (_local2 < this.combos.length) {
                if (this.combos[_local2] == _arg1){
                    this.combos.splice(_local2, 1);
                    break;
                };
                _local2++;
            };
        }
        private function addedToStage(_arg1:Event):void{
            if (!this.onStage){
                this.onStage = true;
                stage.addEventListener(KeyboardEvent.KEY_DOWN, this.keyDown);
                stage.addEventListener(KeyboardEvent.KEY_UP, this.keyUp);
            };
        }
        private function removedFromStage(_arg1:Event):void{
            if (this.onStage){
                this.onStage = false;
                stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.keyDown);
                stage.removeEventListener(KeyboardEvent.KEY_UP, this.keyUp);
            };
        }
        private function keyDown(_arg1:KeyboardEvent):void{
            var _local3:KeyCombo;
            var _local2:int;
            while (_local2 < this.combos.length) {
                _local3 = this.combos[_local2];
                _local3.keyDown(_arg1.keyCode);
                _local2++;
            };
        }
        private function keyUp(_arg1:KeyboardEvent):void{
            var _local3:KeyCombo;
            var _local2:int;
            while (_local2 < this.combos.length) {
                _local3 = this.combos[_local2];
                _local3.keyUp(_arg1.keyCode);
                _local2++;
            };
        }

    }
}
